package com.yonyou.cyxdms.sysAuth.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyxdms.sysAuth.service.dto.ConsultantWorkDTO;
import com.yonyou.cyxdms.sysAuth.service.service.ConsultantWorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author tanjie
 * @since 2019-08-14
 */
@RestController
@RequestMapping("/consultantWork")
@Api("顾问线索分配相关接口")
public class ConsultantWorkController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ConsultantWorkController.class);

    @Autowired
    public ConsultantWorkService consultantWorkService;

    /**
     * 分页查询数据
     *
     * @param queryCondition 查询条件
     * @return com.baomidou.mybatisplus.core.metadata.IPage 分页对象
     * @author tanjie
     * @since 2019-08-14
     */
    @ResponseBody
    @GetMapping
    @SuppressWarnings("unchecked")
    public IPage<Map<String, Object>> getList(@RequestParam Map<String, Object> queryCondition, @RequestParam
            ("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {
        Page<Map<String, Object>> page = new Page(currentPage, pageSize);
        return consultantWorkService.selectPageBysql(page, queryCondition);
    }

    /**
     * 进行数据修改
     *
     * @param id 数据主键ID
     * @return Map<String, Object> 数据对应的map 结构
     * @author tanjie
     * @since 2019-08-14
     */
    @ResponseBody
    @GetMapping(value = "/{id}")
    @SuppressWarnings("unchecked")
    public Map<String, Object> getById(@PathVariable("id") Long id) {
        return consultantWorkService.getById(id);
    }

    /**
     * 进行数据新增
     *
     * @param consultantWorkDTO 需要保存的DTO
     * @return Map<String, Object> 返回的数据map
     * @author tanjie
     * @since 2019-08-14
     */
    @ResponseBody
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    @SuppressWarnings("unchecked")
    public Map<String, Object> insert(@RequestBody ConsultantWorkDTO consultantWorkDTO) {
        logger.debug("consultantWorkDTO:{}", consultantWorkDTO);
        return consultantWorkService.insert(consultantWorkDTO);
    }

    /**
     * 进行数据修改
     *
     * @param id                需要修改数据的ID
     * @param consultantWorkDTO 需要保存的DTO
     * @return Map<String, Object> 返回的数据map
     * @author tanjie
     * @since 2019-08-14
     */
    @ResponseBody
    @PutMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    @SuppressWarnings("unchecked")
    public Map<String, Object> update(@PathVariable("id") Long id, @RequestBody ConsultantWorkDTO consultantWorkDTO) {
        logger.debug("consultantWorkDTO:{}", consultantWorkDTO);
        return consultantWorkService.update(id, consultantWorkDTO);
    }

    /**
     * 根据id删除对象
     *
     * @param id 需要修改数据的ID
     * @author tanjie
     * @since 2019-08-14
     */
    @DeleteMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public boolean deleteById(@PathVariable("id") Long id) {
        logger.debug("删除ID:{}", id);
        consultantWorkService.deleteById(id);
        return true;
    }

    /**
     * 根据IDs批量删除对象
     *
     * @param ids 需要修改数据的ID集合
     * @author tanjie
     * @since 2019-08-14
     */
    @DeleteMapping(value = "/batch/{ids}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public boolean deleteByIds(@PathVariable("ids") String ids) {
        logger.debug("删除ID:{}", ids);
        consultantWorkService.deleteBatchIds(ids);
        return true;
    }

    /**
     * 通过经销商CODE查询出所有当天工作的顾问与设置的权重
     *
     * @param:
     * @return: Map<顾问id, 权重比例>
     * @auther: tanjie
     * @since: 2019/8/14
     */
    @GetMapping("/getWorkConsultant")
    public Map<String, Integer> getWorkConsultantByDealerCode(@RequestParam("ownerCode") String ownerCode) {
        return consultantWorkService.getWorkConsultantByDealerCode(ownerCode);
    }

    /**
     * 通过经销商CODE查询出所有当天工作的顾问与已经分配线索的权重
     *
     * @param:
     * @return: Map<顾问id, 当前权重比例>
     * @auther: tanjie
     * @since: 2019/8/14
     */
    @GetMapping("/getWorkConsultantRatio")
    public Map<String, Integer> getWorkConsultantRatioByDealerCode(@RequestParam("ownerCode") String ownerCode) {
        return consultantWorkService.getWorkConsultantRatioByDealerCode(ownerCode);
    }

    /**
     * 对当前顾问的线索数进行更新
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/8/15
     */
    @PutMapping("updateConsultant")
    public Integer updateConsultantClues(@RequestParam("consultantId") String consultId,
                                         @RequestParam("num") Integer num) {
        return consultantWorkService.updateConsultantClues(consultId, num);
    }

    /**
     * 通过顾问ID修改顾问工作时间
     *
     * @param workDay 服务时间[1,3,4]
     * @return:
     * @auther: tanjie
     * @since: 2019/8/15
     */
    @ApiOperation("修改顾问工作时间")
    @PutMapping("/ConsultWorkTime")
    public Integer updateConsultWorkTime(@ApiParam("顾问ID") @RequestParam("consultantId") String consultId,
                                         @ApiParam("顾问工作时间") @RequestParam("workDayByWeek") List<Integer> workDay) {
        return consultantWorkService.updateConsultWorkTime(consultId, workDay);
    }

    /**
     * 通过顾问ID修改顾问权重
     *
     * @param: map<顾问id   ,       顾问权重> 权重以百分展示
     * @return:
     * @auther: tanjie
     * @since: 2019/8/15
     */
    @ApiOperation("修改顾问权重")
    @PutMapping("/consultRatio")
    public Integer updateConsultRatio(@RequestBody HashMap<String, Integer> map) {
        logger.info("进行顾问权重修改");
        return consultantWorkService.updateConsultRatio(map);
    }


    /**
     * 查询此经销商下所有顾问及其分配信息
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/8/28
     */
    @ApiOperation("获取顾问及其分配信息")
    @GetMapping("/consultWorkInfo")
    public List<Map<String, Object>> consultantWorkInfo() {
        logger.info("获取经销商的顾问分配信息");
        return consultantWorkService.consultantWorkInfo();
    }


    /**
     * 查询此经销商下所有未分配的顾问信息
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/8/28
     */
    @ApiOperation("获取未分配的顾问")
    @GetMapping("/consultWorkInfoNoAllot")
    public List<Map<String, String>> consultantWorkNotAllotInfo(@RequestParam(value = "consultantId", required = false) String consultId) {
        logger.info("获取经销商的顾问分配信息");
        return consultantWorkService.consultantWorkNotAllotInfo(consultId);
    }

    /**
     * 删除顾问配置信息
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/8/29
     */
    @ApiOperation("删除该顾问配置信息")
    @DeleteMapping("delConsult/{consultId}")
    //@ResponseStatus(HttpStatus.NO_CONTENT)
    public Boolean delConsultWork(@PathVariable("consultId") String consultId) {
        return consultantWorkService.delConsultWorkById(consultId);
    }

    /**
     * 新增加顾问分配信息
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/8/30
     */
    @ApiOperation("新增加顾问分配信息")
    @PostMapping("/addConsult")
    @ResponseStatus(HttpStatus.CREATED)
    public Boolean addConsulWork(@RequestBody ConsultantWorkDTO consultantWorkDTO) {
        logger.info("添加顾问分配信息");
        return consultantWorkService.addConsultWork(consultantWorkDTO);
    }


    @ApiOperation("修改顾问分配信息")
    @PutMapping("/updConsult")
    @ResponseStatus(HttpStatus.CREATED)
    public Boolean updateConsult(@RequestBody ConsultantWorkDTO consultantWorkDTO) {
        logger.info("进行顾问分配信息修改");
        return consultantWorkService.updateConsult(consultantWorkDTO);
    }


}